
var comm = {}; 
// https://www.example.com为演示域名，上线前请修改成自己的域名并开通ssl认证。
// addons/youyicard 为接口目录，没有代码基础请不要删除或者修改。 
// var baseUrl = "https://yundianzituce.lsdzws.com/addons/youyicard"
var baseUrl = "http://127.0.0.1/index.php/addons/youyicard/"
comm.baseUrl = baseUrl
comm.cjRequest = (params) => {  
    var header = { 
       // 'content-type': 'application/json',
      'content-type':'application/x-www-form-urlencoded'
    };
    if (params.token) {
      var userInfo =  wx.getStorageSync("userInfo"); 
      header.token = userInfo.token 
    } 
    return new Promise((resolve, reject) => { 
        wx.request({  
            url: baseUrl + params.url,
            data:params.data, 
            header: header,
            method:'POST',
            success: (result) => { 
              var data = result.data; 
               if(data.code == 1000){
                wx.showToast({
                  title: data.msg,
                  icon:'none'
                }) 
                wx.clearStorage('userInfo');
                setTimeout(function(){ 
                  wx.navigateTo({
                    url: '/pages/loginaccount/index',
                  })
                },1200)
                return false;
              }else{
                resolve(result)
              }
                // 请求成功，就将成功的数据返回出去
                
            },
            fail: (err) => {
                reject(err)
            },
            // 不管请求成功还是失败，都会触发
            complete: () => {
              
            }
        }); 
    })
}

comm.goLoginPage = function(msg = '抱歉，请先登录'){
  wx.showToast({
    title: msg,
    icon:'none'
  })
  wx.clearStorage();
  setTimeout(function(){ 
    wx.navigateTo({
      url: '/pages/loginaccount/index',
    })
  },1000)
  
} 

// 从微信选择上传文件 
comm.upload = function(url, filePath,avatar = null) { 
  var userInfo =  wx.getStorageSync("userInfo");
  if(!userInfo){
    setTimeout(function(){ 
      wx.navigateTo({
        url: '/pages/loginaccount/index',
      })
    },1000)
    return false;
  }
  
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: baseUrl + url,
      filePath: filePath,
      name: 'file',
      header: { 
        'content-type': 'application/json', 
        'token':userInfo.token,
        'avatar':avatar
      },
      success: (res) => { 
        var data = JSON.parse(res.data);
        if(data.code == 1000){
          wx.showToast({
            title: data.msg,
            icon:'none'
          }) 
          wx.clearStorage('userInfo'); 
          setTimeout(function(){
            wx.navigateTo({
              url: '/pages/loginaccount/index',
            })
          },1000)
          return false;
        }
        console.log()
        resolve(data);
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 处理富文本里的图片宽度自适应
 * 1.去掉img标签里的style、width、height属性
 * 2.img标签添加style属性：max-width:100%;height:auto
 * 3.修改所有style里的width属性为max-width:100%
 * 4.去掉<br/>标签
 * 修改字体样式
 * @param html
 * @returns {void|string|*}
 */
comm.formatRichText = function(html){ 
  let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
      match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
      match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
      match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
      return match;
  });
  newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
      match = match
      .replace(/<p>/gi, '<p class="p_class">')
      .replace(/width:[^;]+;/gi, 'max-width:100%;')
      .replace(/width:[^;]+;/gi, 'max-width:100%;');
      return match;
  }); 
  newContent = newContent.replace(/<br[^>]*\/>/gi, "");
  newContent = newContent.replace(/<a>/gi, '<a class="p_class "');
  newContent = newContent.replace(/<li>/gi, '<li class="p_class "');
  newContent = newContent.replace(/\<p/gi, '<p class="p_class "');
  newContent = newContent.replace(/\<span/gi, '<span class="p_class "');
  newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'); 
  return newContent;
}




export {
  comm
}